﻿using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.Api;
using JESAI.Application.Abstractions.Dtos;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core.Attributes;
using JESAI.Core.Extensions;
using JESAI.Platform.Domain.Admin.Entities;
using JESAI.Platform.Domain.Admin.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Admin
{
    [AppService]
    public class ApiService : IApiService
    {
        private readonly IApiRepository _apiRepository;
        private readonly IRoleApiRepository _roleApiRepository;
        public ApiService(IApiRepository apiRepository, IRoleApiRepository roleApiRepository)
        {
            _apiRepository = apiRepository;
            _roleApiRepository = roleApiRepository;
        }

        public void DeleteApi(long id)
        {
            _apiRepository._DbQueryable.Context.Ado.BeginTran();
            try
            {
                _apiRepository.DeleteById(id);
                _roleApiRepository.Delete(p => p.ApiId == id);
                _apiRepository._DbQueryable.Context.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _apiRepository._DbQueryable.Context.Ado.RollbackTran();
                throw new Exception("事务执行失败", ex);
            }
        }

        public void DeleteApis(long[] ids)
        {
            _apiRepository._DbQueryable.Context.Ado.BeginTran();
            try
            {
                _apiRepository.DeleteByIds(ids as dynamic);
                _roleApiRepository.Delete(p => ids.Contains(p.ApiId));
                _apiRepository._DbQueryable.Context.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _apiRepository._DbQueryable.Context.Ado.RollbackTran();
                throw new Exception("事务执行失败", ex);
            }
        }


        public BasePageOutput<List<ApiDto>> QueryApiList(QueryApiListInput input)
        {
            var totalNumber = 0;
            var modelList = _apiRepository._DbQueryable
                      .WhereIF(!input.ProjectKey.IsEmpty(), it => it.ProjectName == input.ProjectKey)
                      .ToPageList(input.PageIndex, input.PageSize, ref totalNumber);

            return new BasePageOutput<List<ApiDto>> { Data = modelList.MapTo<List<ApiDto>>(), CurrentPage = input.PageIndex, Total = totalNumber };
        }

        public List<ApiDto> QueryApis(QueryApisInput input)
        {
            var modelList = _apiRepository._DbQueryable
                    .WhereIF(!input.ControllerName.IsEmpty(), it => it.ProjectName == input.ControllerName).ToList();

            return modelList.MapTo<List<ApiDto>>();
        }

        public void UpdateOrInsert(ApiDto apiDto)
        {
            ApiModel apiModel = apiDto.MapTo<ApiModel>();
            if (apiModel.Id > 0)
            {
                apiModel.UpdateTime = DateTime.Now;
                _apiRepository.AsUpdateable(apiModel).IgnoreColumns(it => new { it.CreateTime }).ExecuteCommand();
            }
            else
            {
                apiModel.UpdateTime = DateTime.Now;
                apiModel.CreateTime = DateTime.Now;
                _apiRepository.AsInsertable(apiModel).ExecuteCommand();
            }
        }

        public void UpdateOrUpdate(ApiDto apiDto)
        {
            ApiModel apiModel = apiDto.MapTo<ApiModel>();
            if (apiModel.Id > 0)
            {
                apiModel.UpdateTime = DateTime.Now;
                _apiRepository.AsUpdateable(apiModel).IgnoreColumns(it => new { it.CreateTime }).ExecuteCommand();
            }
            else
            {
                apiModel.UpdateTime = DateTime.Now;
                apiModel.CreateTime = DateTime.Now;
                _apiRepository.AsInsertable(apiModel).ExecuteCommand();
            }
        }
    }
}
